<?php
	session_start();
	include("write_functions.php");
	connect("localhost", "root", "", "gradegriddatabase");
?>
<!--
Allows a teacher to edit a grading template, or to make a new grading
template from an old one (sure?).

@author Michael Argarin
@version Second Semester, AY 2010-2011
-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>GradeGrid - Edit Template</title>
<script src="template.js" type="text/javascript"></script>
</head>
<?php include("fetchData.php");?>
<body onload="window.resizeTo(760,500)">
	<h2>Edit Template<br/><span style="font-size:11px;">NOTE: All fields are REQUIRED. Percentages and max raw scores must be numbers and percentages must total to 100.</span></h2>
    <form action="modifyTemplate.php?template_id=<?php echo $_GET['template_id'];?>" method="post">
<?php if($_GET['retry']!=1){?>
	<?php 
		$result=fetchTemplateDetails($_GET['template_id']);
		$data=mysql_fetch_array($result);
	?>
    	<table>
        	<tr>
            	<td>Template Name</td>
                <td><input name="template" type="text" value="<?php echo $data['template_name'];?>"></td>
            </tr>
        </table>
        Major Requirements <input type="button" onclick="addMaj()" value="Add more">
        <ul><table id="maj" border="1" align="center" style="text-align:center">
        	<?php $majorIndex=0;
			do{
				$maj=$data['major_id'];
				echo
                "<tr>
                    <td><li>Name</li></td>
                    <td><input name=\"majName".$majorIndex."\" type=\"text\" value=\"".$data['major_name']."\"></td>
                    <td>Percentage</td>
                    <td><input name=\"majPer".$majorIndex."\" type=\"text\" value=\"".$data['percentage']."\"></td>";
				if($majorIndex!=0)
					echo "<td rowspan=\"3\"><input type=\"button\" value=\"Remove\" onclick=\"remMaj(".$majorIndex.")\"></td></tr>";
			?>
            <tr><td colspan="4"><li>Minor Requirements <input type="button" onclick="addMin(<?php echo $majorIndex;?>)" value="Add more"></li></td></tr>
            <?php
				$minorIndex=0;
				echo "<tr><td colspan=\"4\"><table id=\"min".$majorIndex."\">";
				do{
					if($data['major_id'] != $maj)
						break;
					echo
					"<tr>
						<td><ul><li>Name</li></ul></td>
						<td><input name=\"minName".$majorIndex.$minorIndex."\" type=\"text\" value=\"".$data['minor_name']."\"></td>
						<td>Max Raw Score</td>
						<td><input name=\"minSco".$majorIndex.$minorIndex."\" type=\"text\" value=\"".$data['maxrawscore']."\"></td>";
					if($minorIndex!=0)
						echo "<td><input type=\"button\" value=\"Remove\" onclick=\"remMin(".$majorIndex.",".$minorIndex.")\"></td></tr>";
					else
						echo "</tr>";
					$minorIndex++;
				}while($data = mysql_fetch_array($result));
				echo "</table></td></tr>";
				$majorIndex++;
            }while($data);?>
            <tr><td colspan="5" align="right"><input type="submit" value="Save"></td></tr>
        </table></ul>
    </form>
<?php } else { include("retryTemplate.php"); }?>
	<div style="font-size:9px; width:100%" align="center">GradeGrid Copyright</div>
<?php mysql_close();?>
</body>
</html>